package Controller.Goods;
import Dto.Goods;
import Service.GoodsService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.time.LocalDateTime;

@MultipartConfig
@WebServlet(name = "addGoods", value = "/addGoods")
public class AddGoods extends HttpServlet {
    private GoodsService goodsService = new GoodsService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");


//        Integer id = request.getParameter("id");
        String goodscategory = request.getParameter("goodscategory");
        String name = request.getParameter("name");
        String describe = request.getParameter("describe");
        String picture = request.getParameter("picture");
        String oldprice = request.getParameter("oldprice");
        String newprice = request.getParameter("newprice");
        String salenumber = request.getParameter("salenumber");
        String inventory = request.getParameter("inventory");
        String status = request.getParameter("status");
        String addtime = request.getParameter("addtime");
//        LocalDateTime nowDate = LocalDateTime.now();
        Goods goods = new Goods();
//        goods.setId(id);
        goods.setGoodscategory(goodscategory);
        goods.setName(name);
        goods.setDescribe(describe);
        goods.setPicture(picture);
        goods.setOldprice(oldprice);
        goods.setNewprice(newprice);
        goods.setSalenumber(salenumber);
        goods.setInventory(inventory);
        goods.setStatus(status);
        goods.setAddtime(addtime);
//        goods.setAddtime(nowDate.toString());
        try {
            goodsService.addGoods(goods);
            request.getRequestDispatcher("/ListGoods").forward(request,response);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}